package com.pr.makfilm.service.impl;

import com.pr.makfilm.dao.IAbstractDao;
import com.pr.makfilm.dao.ISchoolDao;
import com.pr.makfilm.domain.*;
import com.pr.makfilm.security.service.ISecurityService;
import com.pr.makfilm.service.IDistrictService;
import com.pr.makfilm.service.ISchoolService;
import com.pr.makfilm.service.ISchoolTypeService;
import com.pr.makfilm.service.util.SidUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Pawel
 * Date: 10.09.13
 * Time: 22:54
 * To change this template use File | Settings | File Templates.
 */
@Service(SchoolServiceImpl.BEAN_NAME)
public class SchoolServiceImpl extends AbstractServiceImpl<School> implements ISchoolService
{
   public static final String BEAN_NAME = "schoolService";

   @Resource
   private ISchoolDao schoolDao;

   @Resource
   private ISchoolTypeService schoolTypeService;

   @Resource
   private IDistrictService districtService;

   @Resource
   private ISecurityService securityService;

   @Override
   protected IAbstractDao<School> getDao()
   {
      return schoolDao;
   }

   @Override
   @Transactional
   public School save(School school)
   {
      school.setSid(SidUtils.generate());
      school.setObjectState(ObjectStateEnum.ACTIVE);
      school.setUser(securityService.getLoggedInUser());
      SchoolType type = school.getType();
      if (type != null)
      {
         school.setType(schoolTypeService.getBySid(type.getSid()));
      }
      District district = school.getDistrict();
      if (district != null)
      {
         school.setDistrict(districtService.getBySid(district.getSid()));
      }
      return super.save(school);
   }

   @Override
   @Transactional
   public List<School> findByType(SchoolType type)
   {
      User user = securityService.getLoggedInUser();
      return schoolDao.findByType(type, user);
   }

   @Override
   @Transactional
   public List<String> findSchoolCities()
   {
      User user = securityService.getLoggedInUser();
      return schoolDao.findSchoolCities(user);
   }
}
